//
//  VCInformationTool.m
//  VCDemo
//
//  Created by 肖康龙 on 2017/9/5.
//  Copyright © 2017年 vsoontech. All rights reserved.
//

#import "VCInformationTool.h"
#import "VCDowmload.h"



@implementation VCInformationTool
static NSDictionary *VCDict_file;
static NSMutableArray *array_tsNum;//每一个ts片段谁下载下来的
static NSMutableArray *array_ts;

/**保存信息*/
+(void)saveFileDict:(NSDictionary *)dict{
    VCDict_file = dict;
}
/**获取列表信息*/
+(NSDictionary *)getFileDict{
    return VCDict_file;
}
/**保存当前缓存的ts列表
 *tsName
 *isReset 是否重置
 *tsNum 谁下载完毕的
 */
+(void)saveTSFileWithTsName:(NSString *)tsName isReset:(BOOL)isReset tsNum:(NSUInteger)tsNum{
    if (array_ts == nil) {
        array_ts = [NSMutableArray array];
        array_tsNum = [NSMutableArray array];
    }
    if (isReset) {//清零
        [array_ts removeAllObjects];
        [array_tsNum removeAllObjects];
    }else{
        [array_ts addObject:tsName];
        [array_tsNum addObject:@(tsNum)];
        if (array_ts.count>5) {
            
            [self deleteFileWithPath:[array_ts objectAtIndex:0]];
            [array_ts removeObjectAtIndex:0];
        }
    }
    
}
/**获取当前缓存的ts列表*/
+(NSArray *)getTSFileArray{
    return  array_ts;
}

/**获取当前缓存缓存是哪个下载完成的
 *index 第几个ts
 *fileName tsid
 */
+(NSUInteger)getTSDownloadNumWithIndex:(NSUInteger)index withFileName:(NSString *)fileName{
    if (array_tsNum.count > index || fileName == nil) {
        NSArray *array = array_ts;
        for (NSString *path in array) {
            if ([path containsString:fileName]) {
                return [array_tsNum[index] unsignedIntegerValue];
            }
        }
        return 0;
    }else{
        return 0;
    }
}

// 删除沙盒里的文件
+(void)deleteFileWithPath:(NSString *)path {
    NSFileManager* fileManager=[NSFileManager defaultManager];
    
    BOOL blHave=[fileManager fileExistsAtPath:path];
    if (!blHave) {
        VCNSLog(@"no  have");
        return ;
    }else {
        VCNSLog(@" have");
        BOOL blDele= [fileManager removeItemAtPath:path error:nil];
        if (blDele) {
            VCNSLog(@"dele success");
        }else {
            VCNSLog(@"dele fail");
        }
        
    }
}

@end
